package com.kilograms.lensflareit;

import java.io.File;
import java.util.ArrayList;

import android.app.Activity;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class Browser extends Activity implements OnItemClickListener {

	private static String TAG = "com.kilograms.lensflareit.Browser";

	File extStorage;
	ArrayList<File> files;
	File currentFile;

	boolean externalStorageAvailable = false;
	boolean externalStorageWriteable = false;
	String state;

	LinearLayout layout;
	TextView pathTxt;
	ListView listView;

	ArrayAdapter<File> adapter;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.browserlayout);
		setTitle("FILE BROWSER");

		layout = (LinearLayout) findViewById(R.id.browserlayout);
		pathTxt = (TextView) findViewById(R.id.pathtext);
		listView = (ListView) findViewById(R.id.filelist);

		init();
	}

	private void init() {
		state = Environment.getExternalStorageState();
		if (Environment.MEDIA_MOUNTED.equals(state)) { // Readable and writable
			externalStorageAvailable = externalStorageWriteable = true;
		} else if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) { // Read
			// only
			externalStorageAvailable = true;
			externalStorageWriteable = false;
		} else {
			externalStorageAvailable = externalStorageWriteable = false;
		}

		extStorage = Environment.getExternalStorageDirectory();
		pathTxt.setText(extStorage.getPath());
		Log.v(TAG, "extStorage: " + extStorage.getPath());
		Log.v(TAG, "Files: " + extStorage.listFiles().length);
		
		// Add files
		files = new ArrayList<File>();
		File[] temp = extStorage.listFiles();
		for (int i = 0; i < temp.length; i++) {
			Log.v(TAG, "File " + i + ": " + temp[i]);
			files.add(temp[i]);
		}
		adapter = new ArrayAdapter<File>(this, R.layout.file_list_item, files);

		listView.setAdapter(adapter);
		listView.setOnItemClickListener(this);
	}
	
	private void changeDirectory(File newDirectory) {
		currentFile = newDirectory;
		files.clear();
		// Add files
		File[] temp = currentFile.listFiles();
		for (int i = 0; i < temp.length; i++) {
			files.add(temp[i]);
		}
		adapter.notifyDataSetChanged();
//		Log.v(TAG, "Files::");
//		for (int i = 0; i < files.size(); i++) {
//			Log.v(TAG, "File: " + files.get(i));
//		}
//		Log.v(TAG, "Adapter items::");
//		for (int i = 0; i < adapter.getCount(); i++) {
//			Log.v(TAG, "Item: " + adapter.getItem(i));
//		}
		pathTxt.setText(currentFile.getPath());
	}
	
	public void onItemClick(AdapterView<?> parent, View view, int pos, long id) {
		Toast.makeText(getApplicationContext(), ((TextView) view).getText(),
				Toast.LENGTH_SHORT).show();
		String pathString = (String) ((TextView) view).getText();
		File temp = new File(pathString);
		if(temp.isDirectory()) {
			changeDirectory(temp);			
		} else { // Temp is a file
			
		}
	}

	@Override
	public void onBackPressed() {
		if(currentFile.getPath().equals(extStorage.getPath()) || currentFile.getParent()==null) {
			super.onBackPressed();
		} else {
			changeDirectory(currentFile.getParentFile());
			Toast.makeText(getApplicationContext(), currentFile.toString(), Toast.LENGTH_SHORT).show();
		}
	}
}
